<div ng-controller="Purchasing_MyDetailPurchaseTypeC">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">直发采购单详情</h1>
    </div>
    <div class="wrapper-md">
        <div class="panel panel-default">
            <div class="table-responsive">
                <form name="form" class="form-validation">
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <div class="form-group">
                                <label>供应商</label>
                                <input type="text" class="form-control" readonly="readonly" value="{{orderdetaildata.Provider.Name}}">
                            </div>
                            <div class="form-group">
                                <label for="suojia" style="margin-right: 10px;">货款是否标件</label>
                                <label class="i-checks">
                                    <input type="checkbox" ng-disabled="orderdetaildata.State != 2 && orderdetaildata.State != 0" id="suojia" ng-model="orderdetaildata.IsStandard">
                                    <i></i>
                                </label>
                                <label for="zipai" style="margin-right: 10px;" ng-click="zipai()">运费是否标件</label>
                                <label class="i-checks" ng-click="zipai()">
                                    <input type="checkbox" ng-disabled="orderdetaildata.State != 2 && orderdetaildata.State != 0" id="zipai" ng-model="orderdetaildata.IsPublicDelivery">
                                    <i></i>
                                </label>
                            </div>
                            <div class="form-group">
                                <label>每吨运费</label>
                                <input type="text" class="form-control" ng-disabled="orderdetaildata.State != 2 && orderdetaildata.State != 0" ng-change="sum()" ng-model="orderdetaildata.DeliveryPrice" >
                            </div>
                            <div class="form-group">
                                <label>联系人信息</label>
                                <input ng-disabled="orderdetaildata.State == 3" type="text" class="form-control" ng-model="orderdetaildata.Contact">
                            </div>
                            <div class="form-group">
                                <label>备注</label>
                                <input type="text" ng-disabled="orderdetaildata.State == 3" class="form-control" ng-model="orderdetaildata.Remarks">
                            </div>
                            <div class="form-group">
                                <label>创建人：</label>
                                <span>{{orderdetaildata.Creater.Name}}</span>
                            </div>
                            <div class="form-group">
                                <label>采购单状态：</label>
                                <span ng-if="orderdetaildata.State == 0">待确认</span>
                                <span ng-if="orderdetaildata.State == 1">审批中</span>
                                <span ng-if="orderdetaildata.State == 2">采购中</span>
                                <span ng-if="orderdetaildata.State == 3">取消</span>
                                <span ng-if="orderdetaildata.State == 4">完成采购</span>
                                <span ng-if="orderdetaildata.State == 5">入库审批</span>
                                <span ng-if="orderdetaildata.State == 6">已入库</span>
                                <span ng-if="orderdetaildata.State == 7">已发货</span>
                            </div>
                            <div class="form-group">
                                <label>对应直发计划单ID：</label>
                                <span>{{orderdetaildata.FromId}}</span>
                            </div>
                        </div>
                        <div class="container-fluid text-center bg-light lter" >
                            <div class="row">
                                <h3 style="display: inline-block;margin: 0 auto">采购单商品列表</h3>
                            </div>
                        </div>
                        <div class="container-fluid" style="padding-top: 10px">
                            <div class="panel panel-default" ng-repeat="item in orderdetaildata.Goodses" style="margin-bottom: 10px">
                                <div class="panel-heading">
                                    <div>品牌：{{item.Commodity.Brand}}</div>
                                </div>
                                <div class="panel-body">
                                    <div class="row">
                                        <div class="col-xs-9 col-md-9">
                                            <div>名称/材质/规格：{{item.Commodity.Name}}/{{item.Commodity.Standard}}/{{item.Commodity.Format}}</div>
                                            <div>单价：
                                                <input ng-change="sum()" ng-disabled="orderdetaildata.State == 4 || orderdetaildata.State == 7" type="number" min="0" ng-model="item.Price">/t
                                            </div>
                                            <div>数量：{{item.Count}}
                                            </div>
                                            <div>总质量：<input ng-change="sum()" ng-disabled="orderdetaildata.State == 4 || orderdetaildata.State == 7" type="number" min="0" ng-model="item.Weight">t</div>
                                        </div>
                                    </div>
                                </div>
                                <div class="panel-footer">
                                    <div>总价：{{item.Price | number:2}}*{{item.Weight | number:3}}t={{item.Price*item.Weight | number:2}}</div>
                                </div>
                            </div>
                            <div class="row text-left container-fluid">
                                <h4 style="display: inline-block">总价：{{orderdetaildata.Total | number:2}}</h4>
                            </div>
                            <div class="row text-left container-fluid">
                                <h4 style="display: inline-block">预计运费价格：{{newWeight * orderdetaildata.DeliveryPrice | number:2}}</h4>
                            </div>
                            <div class="row text-right">
                                <button ng-if="orderdetaildata.State == 0|| orderdetaildata.State == 1|| orderdetaildata.State == 2" type="button" class="btn btn-success" ng-click="cancel()">取消采购单</button>


                                <button type="button" ng-if="orderdetaildata.State == 2 && orderdetaildata.State !== 3 || orderdetaildata.State == 0 && orderdetaildata.State !== 3" class="btn btn-success" ng-disabled="form.$invalid || !isedit" ng-click="ordersub()">确认修改</button>
                                <button type="button" ng-if="orderdetaildata.State != 2 && orderdetaildata.State != 3 && orderdetaildata.State != 0" class="btn btn-success" ng-disabled="form.$invalid || !isedit" ng-click="ordersub1()">确认修改</button>
                                <button type="button" ng-if="orderdetaildata.State == 2" class="btn btn-success" ng-click="clearing()">确认完成</button>
                                <button type="button" ng-if="orderdetaildata.State == 5 || orderdetaildata.State == 6 || orderdetaildata.State == 7 || orderdetaildata.State == 4" class="btn btn-success" ng-click="changeorderdata2()">设置实际采购价格</button>
                                <button type="button" class="btn btn-success" ng-click="goOrder(orderdetaildata.FromId)">跳转到对应计划单</button>
                                <button type="button" class="btn btn-success" ng-click="goorderList()">返回采购单列表</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    angular.module('app')
            //采购单详情控制器
            .controller("Purchasing_MyDetailPurchaseTypeC", function ($scope, $state, $stateParams, $http) {
                $scope.newWeight = 0;
                if($stateParams.type == 0){
                    $scope.id = JSON.parse($stateParams.orderid);
                    //获取采购单详细数据
                    $http({
                        url: "../api/Purchase/" + $scope.id,
                        method: "GET"
                    })
                            .then(function (res) {
                                console.log(res);
                                $scope.orderdetaildata = res.data;
                                console.log($scope.orderdetaildata);
                                $scope.sum()
                            })
                            .then(function (error) {
                                if (error) {
                                    console.log("error");
                                }
                            });
                }else if($stateParams.type == 1){
                    $http({
                        url: "../api/Purchase/DetailsByFromId?fromId=" + JSON.parse($stateParams.orderid),
                        method: "GET"
                    })
                            .then(function (res) {
                                console.log(res);
                                $scope.orderdetaildata = res.data;
                                console.log($scope.orderdetaildata);
                                $scope.sum()
                                $scope.id = res.data.Id;
                            })
                            .then(function (error) {
                                if (error) {
                                    console.log("error");
                                }
                            });
                }

                //采购单确认完成
                $scope.clearing = function () {
                    $http({
                        url: "../api/Purchase/SalePurchaseComplete?pId=" + $scope.id,
                        method: "POST"
                    })
                            .then(function (res) {
                                if (res.State <= 0) {
                                    alert(res.Msg);
                                } else {
                                    alert("确认完成");
                                    window.location.reload();
//                                    $state.go('app.Purchasing_MyDetail', {
//                                        orderid: JSON.stringify($scope.id)
//                                    });
                                    // $state.go('app.Purchasing_MyPurList');
                                }
                            })
                };
                //返回计划单列表
                $scope.goorderList = function () {
                    $state.go('app.Purchasing_MyPurList');
                };
                $scope.isedit = true;
                $scope.edit = function (item) {
                    $scope.isedit = !$scope.isedit;
                    $scope.sum();
                };
                //计算总价方法
                $scope.sum = function () {
                    $scope.newWeight = 0;
                    $scope.orderdetaildata.Total = 0;
                    $scope.Weight = 0;
                    for (var i = 0; i < $scope.orderdetaildata.Goodses.length; i++) {
                        $scope.Weight = $scope.orderdetaildata.Goodses[i].Weight;//$scope.orderdetaildata.Goodses[i].Count *
                        $scope.orderdetaildata.Total += $scope.Weight * $scope.orderdetaildata.Goodses[i].Price;

                        $scope.newWeight += $scope.orderdetaildata.Goodses[i].Weight;
                    }
                    $scope.orderdetaildata.SourceTotal = $scope.orderdetaildata.Total;
                    $scope.orderdetaildata.Total = $scope.orderdetaildata.Total.toFixed(2);
                    $scope.orderdetaildata.DeliveryPriceTotal = $scope.newWeight*$scope.orderdetaildata.DeliveryPrice;
                };
                //保存更改
                $scope.ordersub = function () {
                    $http.post('../api/Purchase/SalePurchaseEdit', $scope.orderdetaildata)
                            .success(function (res) {
                                if (res.State <= 0) {
                                    alert(res.Msg);
                                } else {
                                    alert("保存成功");
                                    window.location.reload();
                                }
                            });
                };
                //保存更改
                $scope.ordersub1 = function () {
                    $http.post('../api/Purchase/PurchaseRemarkEdit', $scope.orderdetaildata)
                            .success(function (res) {
                                if (res.State <= 0) {
                                    alert(res.Msg);
                                } else {
                                    alert("保存成功");
                                    window.location.reload();
                                }
                            });
                };
                //购物车增加数量
                $scope.Countadd = function (item) {
                    item.Count += 1;
                    $scope.sum();
                };
                //购物车减少数量
                $scope.Countdeduct = function (item) {
                    if (item.Count > 0) {
                        item.Count -= 1;
                        $scope.sum();
                    }
                };
                $scope.goOrder = function (id) {
                    $state.go('app.FieldMan_FieldManDetail', {
                        orderid: JSON.stringify(id)
                    });
                };
                //更改商品价格
                $scope.changeorderdata2 = function () {
                    $state.go('app.Purchasing_MyCommodityInfoEdit2', {
                        orderid: JSON.stringify($scope.id),
                        urltype : 0
                    });
                };



                //取消采购单
                $scope.cancel = function () {
                    $http({
                        url: "../api/Purchase/Cancel?id=" + $scope.id,
                        method: "POST"
                    })
                            .then(function () {
                                alert("已取消");
                                $state.go('app.Purchasing_MyPurList');
                                window.location.reload();
                            })
                            .then(function (error) {
                                if (error) {
                                    console.log("error");
                                }
                            });
                };
            })
</script>